<?php
$db = db::getInstance();
$name = new DonneesModel();
$formu = DonneesModel::findByFilter($db, $name);
session_start();
$log = $_SESSION['login'];
$lang=$_SESSION['lang'];
include("views/lang/lang_".$lang.".php");

?>
<html>
    <head>

        <!-- include style css -->
        <link rel="stylesheet" href="../views/css/validationEngine.jquery.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/style_menu.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/mini3537.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/mini74d5.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/template.css" type="text/css"/>
        <link rel="stylesheet" href="../views/css/ui.all.css" type="text/css" media="screen" title="no title" charset="utf-8">
        <link rel="icon" type="image/png" href="../views/image/dynamic_form.png" />

        <!-- include script js -->
        <script type="text/javascript" src="../views/js/jquery-1.6.min.js"></script>
        <script type="text/javascript" src="../views/js/ddaccordion.js"></script>
        <script type="text/javascript" src="../views/js/menu.js"></script>

        <script charset="utf-8" type="text/javascript" src="../views/js/jquery.validationEngine-tests.js"></script>
        <script charset="utf-8" type="text/javascript" src="../views/js/jquery.validationEngine.js"></script>
        <script type="text/javascript" src="../views/js/jquery_upload.js"></script>
        <script src="../views/js/notifier.js" type="text/javascript" charset="utf-8"></script>
        <script src="../views/js/date.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" src="../views/js/gf_scroll_div.js"></script>

        <script>
            
            $(document).ready(function(){	      
            
            
                // fonction appeler pour reduire champ récurssive conditionnel
                function appel (champs)
                {
                    $(".condition").each(function (){
                                                      
                        if ($(this).attr("cond")==champs)
                        {
                            $(this).attr("style","display:none;");
                            $(this).attr('checked', false);
                            
                            //Supp recursive
                            
                            if ($(this).attr("class")=="condition switch voir")
                            {  
                                  
                                var champ="cond_champ"+$(this).attr('id');
                                appel(champ)
                            
                            }                                  
                         
                            if (($(this).attr("cond")!="")&&($(this).attr("class")=="condition has voir validate[required,custom[date]] hasDatepicker"))
                            {
                                
                                $(this).parent("div").attr("style","display:none;");
                                   
                            } 
                                              
                        }   
                                   
                    });
                     
                };
                        
                     
            
                // fonction des conditions 
    
                $(".switch").live("click",function (){
                    var y=$(this).attr('id');
                  
                    if ($(this).is(":checked"))
                    {
                        var champs="cond_champ"+$(this).attr('id');
       
                        $(".condition").each(function(){
                            if ($(this).attr("cond")== champs)
                            {
                                $(this).attr("style","display:block;");
                                
                                $(this).parent("div").attr("style","display:block;");
                                //traitement du champ date 
                                if (($(this).attr("class")=="condition has voir validate[required,custom[date]]")&&($(this).attr("cond")!=""))
                                {
                                   
                                    var x=$(this).attr("id");  
                                    $("#"+x).datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage:'../views/image/calendar.png' });
                                  
                                }          
                                //////////
                            }
                        });
                    }
                    
                    else 
                    {
                        var champs="cond_champ"+$(this).attr('id'); 
                             
                        $(".condition").each(function (){
                                                      
                            if ($(this).attr("cond")==champs)
                            {
                                $(this).attr("style","display:none;");
                                $(this).attr('checked', false);
                            
                                //Supp recursive
                            
                                if ($(this).attr("class")=="condition switch voir")
                                {  
                                  
                                    var champ="cond_champ"+$(this).attr('id');
                                    appel(champ)
                              
                              
                                }
                                  
                                
                                ///////////
                                if (($(this).attr("cond")!="")&&($(this).attr("class")=="condition has voir validate[required,custom[date]] hasDatepicker"))
                                {
                                
                                    $(this).parent("div").attr("style","display:none;");
                                    

                                } 
                                              
                            }   
                                   
                        });
                        
                        
                    }
                   
                }); 
                  
                
                // fonction validation et rassemblement des donnees saisies
                jQuery("#formID").validationEngine('attach',{onValidationComplete: function(form, status){
   
                        if (status == true) {
                            var matrice_val=new Array();
                            var i=-1;
                            $(".voir").each(function(){
                                i++;                           
                              
                             
                                if(($(this).attr("type"))=="file")
                                {
                                                            
                                              
                                }
                              
                                else  if ($(this).is("label"))
                                {
                                    matrice_val[i]=$(this).text();
   
                                }
                                else if ($(this).is(':checked'))
                                {
                                    matrice_val[i]="checked";
                                  
                                }
                                
                                else  
                                {
                                    matrice_val[i]=$(this).val();
                                }
                          
                            });
                    
                    
                            //envoi des donnees
                            $.post("save_data_user",{ contenu :matrice_val,form_name :$("#nom").val(),nb_champ : i, login:'<?php echo $log; ?>'},
                            function(data)
                            { 
                                // traitement relié a laffichage du message 
                                if(data=="well done")            
                    
                                {                  
                                    $.notifier.broadcast({
                     
                                        ttl:'Success ',
                                        msg:'Donnees enregistrer!'
                                    });
                                }
                                else
                                {
                                    $.notifier.broadcast({
                     
                                        ttl:'failed ',
                                        msg:'Vous avez deja remplit ce formulaire!',
                                        skin:'rounded,red'
                                        
                                    });   
                                }    
                            });
                   
                        } 
                    }  
                });
  
                //faire appel a un autre formualaire         
                $(".form_get").live("change",function(){
                
                    var y=$(this).attr('id');                     
                    var x=$(this).parent().attr("id");
                    $("#source_form"+x).html("");                          
                    $.post("form_extract", { name: $("#"+y+" option:selected").val(),extrait :"oui" },
                    function(data) {                       
                        $("#source_form"+x).append(data);
                       
                        // traitement relié au champ date   
                        $(".has").each(function(){
                            if ($(this).attr("cond")=="")
                            {
                                var x=$(this).attr("id");  
                                $("#"+x).datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage:'../views/image/calendar.png' });
                            }
                        });
                        /////////////////////////////////////
                    });
                });
                //construction du menu des formulaires .live("click",function(){
                $(".formulaire").live("click",function(){
                   
                    $.post("form_extract", { name: $(this).attr("name") },
                    function(data) {                       
                        $("#contenu").append(data);
                    
                        // traitement relié au champ date   
                        $(".has").each(function(){
                            if ($(this).attr("cond")=="")
                            {
                                var x=$(this).attr("id");  
                                $("#"+x).datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage:'../views/image/calendar.png' });
                            }
                        });
                        
                        $(".formulaire").each(function(){
                         
                            $(this).attr("disabled","disabled");                            
                        });
                                                            
               
                    }) ; 
                });

            });     
           
        </script>

    </head>

    <body>
        <nav id="main-nav">
        </nav>

        <div id="status-bar">

            <a class="button red" title="Logout" href="../index/login">
                <span class="smaller"><?php echo $bdeconect ?></span>
            </a>

        </div>

        <div class=" block-border" style="float:left" id="test">
            <form id="simple-list-form" class="grey-bg clearfix" action="" method="post">
                <h1>Menu</h1>

                <div class="block-border">
                    <div class="arrowlistmenu" >

                        <!-- Liste-->
                        <h3 class="menuheader expandable"><?php echo $list ?></h3>
                        <ul class="categoryitems favorites no-margin with-tip">

                            <?php
                            $i = 0;
                            foreach ($formu as $name) {
                                $i++;
                                echo'<li><button type="button" id="form' . $i . '"class="formulaire" name="' . $name->getFormName() . '">' . $name->getFormName() . '</button></li>';
                            }
                            ?>
                        </ul>

                        <!-- pied du menu -->
                        <h3 class="menuheader" style="cursor: default"></h3>
                        <div>
<?php echo $comm_user ?>
                        </div>
                    </div>
                </div>    

            </form>


            <div class="block-content no-title dark-bg">
                <p class="mini-infos">
                 <?php echo $hi ?> <b><?php echo $log; ?></b>
                </p>
            </div>
        </div>


        <form id="formID" class="form block-content formular" action="" method="post"   style="position: absolute; right: 200px; top: 120px; width: 700;" enctype="multipart/form-data">


            <div class="block-border" id="contenu">    
                <div id="DIV_MOVE" style="position: absolute; right: 20px; top:0px; left: 110px;">
                    <input class="button" type="submit" value="Envoyer" id="send">
                </div>
            </div>            



        </form>  

    </body>



</html>